package com.example.smartcity_poverty.ui.AllService.LoveDonate

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.getToken
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.model.LoveDonate.LoveBannerModel
import com.example.smartcity_poverty.logic.model.LoveDonate.LoveListAllModel
import com.example.smartcity_poverty.logic.model.LoveDonate.LoveMoreAll
import com.example.smartcity_poverty.logic.model.LoveDonate.LoveSizeAllModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.LoveDonate.activity.ShowActivityListActivity
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_love_donate.*
import kotlinx.android.synthetic.main.re_lovedonae_list.*
import kotlinx.android.synthetic.main.re_lovedonate_item_more.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 爱心捐赠。
 */
class LoveDonateActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_love_donate)
        LoveTb.title = "爱心捐赠"
        LoveTb.setNavigationOnClickListener { finish() }

        // Banner
        setBanner()

        // 捐赠实时列表
        setLoveSizeList()

        // 推荐公益列表项
        setLoveList()
    }

    /**
     * 推荐公益列表项
     */
    private fun setLoveList() {
        val array = ArrayList<LoveListAllModel.RowsBean>()
        if (getToken().isNotEmpty()) {
            GlobalScope.launch(Dispatchers.IO) {
                api.getLoveListAll(getToken()).apply {
                    // 数据填充
                    if (this.code == 200) {
                        this.rows?.let {
                            array.clear()
                            array.addAll(this.rows!!)
                        }

                        runOnUiThread {
                            lListRv.apply {
                                this.layoutManager = LinearLayoutManager(context)
                                this.adapter = LListAdapter(array, R.layout.re_lovedonae_list)
                                this.isNestedScrollingEnabled = false
                            }
                        }
                    } else runOnUiThread { this.msg?.show() }
                }
            }
        } else "请先登录！".show()

    }


    /**
     * 捐赠实时列表
     */
    private fun setLoveSizeList() {
        val array = ArrayList<LoveMoreAll.DataBean>()
        val token =
            getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString()
        if (token.isNotEmpty()) {
            GlobalScope.launch(Dispatchers.IO) {
                api.getLoveMoreAll(token).apply {
                    if (this.code == 200) {
                        array.clear()
                        array.addAll(this.data!!)

                        runOnUiThread {
                            // 传输数据
                            lMoreRv.apply {
                                this.layoutManager = GridLayoutManager(context, 4)
                                this.adapter = MoreAdapter(array, R.layout.re_lovedonate_item_more)
                                this.isNestedScrollingEnabled = false
                            }
                        }
                    } else runOnUiThread { this.msg?.show() }
                }
            }
        } else "请先登录！".show()

    }

    /**
     * 配置轮播图
     */
    private fun setBanner() {
        val array = ArrayList<LoveBannerModel.DataBean>()
        GlobalScope.launch(Dispatchers.IO) {
            api.getLoveBanner(
                getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "")
                    .toString()
            ).apply {
                if (this.code == 200) {
                    array.clear()
                    array.addAll(this.data!!)
                    // 线程
                    runOnUiThread {
                        lBanner.apply {
                            this.adapter =
                                object : BannerImageAdapter<LoveBannerModel.DataBean>(array) {
                                    override fun onBindView(
                                        p0: BannerImageHolder?,
                                        p1: LoveBannerModel.DataBean?,
                                        p2: Int,
                                        p3: Int
                                    ) {
                                        glide(url + p1?.imgUrl.toString(), p0?.imageView!!)
                                    }
                                }
                            this.addBannerLifecycleObserver(this@LoveDonateActivity).indicator =
                                CircleIndicator(context)
                            this.setBannerRound(25f)
                        }
                    }

                }
            }
        }
    }


    /**
     * 服务适配器
     */
    class MoreAdapter(
        val list: MutableList<LoveMoreAll.DataBean>,
        val layoutId: Int
    ) : BaseRecyclerAdapter<LoveMoreAll.DataBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: LoveMoreAll.DataBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val v = holder.viewFind
            val con = v.view?.context

            // 填充数据
            glide(url + d.imgUrl, v.lmoreImage)
            v.lmoreTitle.text = d.name

            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con, ShowActivityListActivity::class.java).putExtra("id", d.id.toString()))
            }
        }

    }


    /**
     * 推荐公益列表项适配器
     */
    class LListAdapter(
        val list: MutableList<LoveListAllModel.RowsBean>,
        val layoutId: Int
    ) : BaseRecyclerAdapter<LoveListAllModel.RowsBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: LoveListAllModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val con = holder.viewFind.view?.context

            // 填充数据
            glide(url + d.imgUrl, holder.viewFind.lListImage)
            holder.viewFind.lListTitle.text = d.name
            holder.viewFind.lListName.text = d.author
            holder.viewFind.lListUserSize.text = "捐赠人数: ${d.donateCount}"
            holder.viewFind.lListUserAll.text = "已筹善款: ${d.moneyNow}"


            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con, ShowActivityListActivity::class.java).putExtra("id", d.id.toString()))
            }
        }

    }

}